package mz.co.research.client.view.component;

import mz.co.research.client.view.model.DataView;

import com.google.gwt.user.client.ui.DecoratedTabPanel;
import com.google.gwt.user.client.ui.Widget;

public abstract class TabSet extends DecoratedTabPanel {

	private DataView dataView;

	private String title;

	private int tabNumber;

	private int startWith;

	public TabSet(DataView dataView, String title, int startWith) {
		this.dataView = dataView;
		this.title = title;
		this.startWith = startWith;
		for (int i = 0; i < startWith; i++) {
			this.addTab();
		}
		this.selectTab(tabNumber - 1);
	}

	public void addTab() {
		this.add(createTab(), title + " (" + this.tabNumber + ")");
		tabNumber++;
	}

	public void delTab() {
		if (tabNumber > startWith) {

			int selectedTab = getTabBar().getSelectedTab();

			this.remove(--tabNumber);

			// Se estava selecionado e vai remover o selecionado
			// deverá ser dado focus a tab anterior.
			if (selectedTab == tabNumber) {
				this.selectTab(tabNumber - 1);
			}
		}
	}

	protected abstract Widget createTab();

	public DataView getDataView() {
		return this.dataView;
	}

	public int getTabNumber() {
		return tabNumber;
	}

}
